package com.ppc.mapper.worker;

import com.github.pagehelper.Page;
import com.ppc.entity.Recording;
import com.ppc.entity.WorkerHandInResult;
import com.ppc.vo.TaskDetailVO;
import com.ppc.vo.TaskPageVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 任务相关的mapper
 *
 * @author Jeferry
 * @date 2023/12/20
 */
@Mapper
public interface WorkerTaskMapper {

  /**
   * 分页查询任务
   *
   * @param taskStatus 任务状态
   * @return {@link Page}<{@link TaskPageVO}>
   */
  Page<TaskPageVO> pageQuery(Integer taskStatus,Long currentWorkerId);

  /**
   * 查询任务详情
   *
   * @param taskId 任务id
   * @return {@link TaskDetailVO}
   */
  TaskDetailVO queryTaskInformation(Long taskId);

  /**
   * 根据标签id查询标签名
   *
   * @param tagId 标签id
   * @return {@link String}
   */
  @Select("select tag_name from tag where tag_id=#{tagId}")
  String selectTagById(Integer tagId);

  /**
   * 更改任务状态
   *
   * @param taskId     任务id
   * @param taskStatus 任务状态
   */
  @Update("update task set task_status=#{taskStatus} where task_id=#{taskId}")
  void changeTaskStatus(Long taskId, Integer taskStatus);

  /**
   * 提交任务
   *
   * @param result 提交结果
   */
  void handinTask(WorkerHandInResult result);

  /**
   * 判断是否已经提交过
   *
   * @param taskId 任务id
   * @param fileId 文件id
   * @return int
   */
  @Select("select count(*) from workerhandinresult where task_id=#{taskId} and file_id=#{fileId}")
  int judgeHandin(Long taskId, Long fileId);

  /**
   * 评论任务
   *
   * @param requireId 需求id
   * @param comment   打分
   */
  @Update("update task set employer_rating=#{comment} where require_id=#{requireId}")
  void commentTask(Long requireId, Integer comment);

  /**
   * 查询任务是否打分过
   *
   * @param taskId 需求id
   * @return {@link Integer}
   */
  @Select("select employer_rating from task where task_id=#{taskId}")
  Integer selectTaskCommented(Long taskId);

  /**
   * 查询任务状态
   *
   * @param taskId 需求id
   * @return {@link Integer}
   */
  @Select("select task_status from task where task_id=#{taskId}")
  Integer queryTaskStatus(Long taskId);

  /**
   * 查询雇主id
   *
   * @param requireId 需求id
   * @return {@link Long}
   */
  @Select("select employer_id from requirement where require_id=#{requireId};")
  Long selectEmployerId(Long requireId);

  /**
   * 查询雇主信用
   *
   * @param employerId 雇主id
   * @return {@link Integer}
   */
  @Select("select employer_credit from employer where employer_id=#{employerId}")
  Integer selectEmployerCredit(Long employerId);

  /**
   * 更改雇主信用
   *
   * @param employerId     雇主id
   * @param employerCredit 雇主信誉
   */
  @Update("update employer set employer_credit=#{employerCredit} where employer_id=#{employerId}")
  void changeEmployerCredit(Long employerId, Integer employerCredit);

  /**
   * 查询需求标题
   *
   * @param requireId 需求id
   * @return {@link String}
   */
  @Select("select require_title from requirement where require_id=#{requireId}")
  String selectRequireName(Long requireId);

  /**
   * 插入记录
   *
   * @param record 记录
   */
  @Insert("insert into recording(user_id, recording_content, recording_type) VALUES (#{userId},#{recordingContent},#{recordingType})")
  void insertIntoRecording(Recording record);

  /**
   * 查询雇主用户id
   *
   * @param employerId 雇主id
   * @return {@link Long}
   */
  @Select("select user_id from employer where employer_id=#{employerId}")
  Long selectEmployerUserId(Long employerId);

  Page<TaskPageVO> pageQueryOnlyClaim( Long currentWorkerId);
  @Select("select require_id from task where task_id=#{taskId}")
  Long selectRequireId(Long taskId);

}
